package com.mdlib.helper.model;


import com.mdlib.helper.model.entity.UserEntity;
import com.mdlib.helper.utils.MDAppUtils;

import java.io.Serializable;

/**
 * Created by librabin on 16/8/25.
 */
public class UserModel implements Serializable, Cloneable {

    public static final String TAG = "user_object";

    private static final long serialVersionUID = 1L;

    private static UserModel instance;

    private String uid;

    private String nickName;

    private String avatarUrl;
    private String phone;


    public static UserModel getInstance() {
        if (instance == null) {
            synchronized (UserModel.class) {
                Object object = MDAppUtils.restoreObject(TAG);
                if (object == null) {
                    object = new UserModel();
                    MDAppUtils.saveObject(TAG, object);
                }

                instance = (UserModel) object;
            }
        }
        return instance;
    }

    public void reset() {
        clearData();
        MDAppUtils.saveObject(TAG, this);
    }

    private void clearData() {
        uid = null;
        nickName = null;
        avatarUrl = null;
        phone = null;
    }

    public void clearCache() {
        clearData();
        MDAppUtils.saveObject(TAG, this);
    }


    public void writeToCache() {
        MDAppUtils.saveObject(TAG, this);
    }


    public void setUserInfo(UserEntity userInfo) {
        this.uid = userInfo.getUid();
        this.nickName = userInfo.getNickName();
        String url=userInfo.getAvatarUrl();
        if(url==null)url="";
        this.avatarUrl = url;
        this.phone = userInfo.getPhone();
        writeToCache();
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}
